from pathlib import Path

import requests

from pathutil import append_path_name

base_url = 'https://leiyang.icu:8443/v1'
storage_url = f'{base_url}/storage/buckets/lybucket/files'
base_headers = {
    'X-Appwrite-Project': 'lyweb'
}


def show_resp(resp):
    try:
        if resp.status_code == 200:
            print(resp.json())
        else:
            print(resp.url)
            print(resp.status_code)
            print(resp.headers)
            print(resp.text)
    except Exception as e:
        print(e)


def upload():
    files = {
        "file": ('appw.py.txt', open(__file__, 'rb')),
    }
    cur_path = Path(__file__)
    resp = requests.post(storage_url,
                         files=files,
                         data={"fileId": append_path_name(cur_path, '.txt').name},
                         headers=base_headers)
    show_resp(resp)


# upload()


def delete():
    resp = requests.delete(f'{storage_url}/appw.py.txt', headers=base_headers,
                           cookies={
                               "a_session_6790c43b003ad7fcd06a": "eyJpZCI6ImZpbGUtZGVsZXIiLCJzZWNyZXQiOiJmOTJkN2ViMDI0ODJlOTZiYjBjNzljNTM3YTU4NTNlMWY0ZmViZDQ3Y2FlYzNlZWMzMTc0OGFjMjYxOTFlM2YyMzcyODU3MWFhZmU2YzVmMmYzZGJhYTBlZTUxOTk0ODhhYTk3MGRhMzRkZjQxYmYwMGNhNWM2NGQwNzBiZDU3M2Q0ZjgyMWE2MjExOGExMGU4NzFmZTVjYWIwNThiYzI0OWQ3ZDQ4YWQ4ZjE1OGIxYjU3NGE5NzNhNDAzYjhlY2ZiYWEwZjI2NTg1OGRlNTFmMjRhZGJiMDlmMjU1YjY0NDFmNGIyMjQxNTRlZDdiMTIxMDQxZWJmNTI1ZTExMGZiIn0="})
    show_resp(resp)


# delete()
def query():
    resp = requests.get(storage_url, headers=base_headers)
    show_resp(resp)


query()


def login():
    url = f'{base_url}/account/sessions'
    resp = requests.post(url, headers=base_headers, json={
        "email": "leiyang-ge@163.com",
        "password": "password"
    })
    show_resp(resp)
# login()
